/**
 * Created by YCXJ-wanglihui on 2015/3/6.
 */
'use strict';

var app = angular.module("tld", []);

app.controller("ChatController", ['$scope', function($scope) {
  $scope.users = [{id:"", name: "王大拿"}];
  $scope.nickName = '小天鹅';
  $scope.send_msg = function() {
    var msg = $scope.msg;
    var nickName = $scope.nickName;
    msg  = nickName + ":" + msg;
    $scope.msg = '';
    $("#content").append("<p>"+msg+"</p>");
    var sio = window.sio;
    sio.emit("msg", msg, function(err, result) {
      if (err) {
        console.info("消息错误");
      } else {
        console.info(result);
      }
    });
  }

  $scope.$watch("nickName", function() {
    var sio = window.sio;
    sio.emit("edit_nick_name", $scope.nickName, function(err, result) {
      if (err) {
        console.info("修改昵称错误");
      } else {
        console.info(result);
      }
    })
  });

  var sio = window.sio;
  sio.on("user_list", function(users) {
      console.info(users);
      $scope.users = users;
      $scope.$apply()
  });
}]);